//
//  EditorAlert.swift
//  YoruNeko
//
//  Created by  mahoPunch on 2021/6/15.
//

import SwiftUI

struct NewBookmarkEditorAlert: View {
    
    //@EnvironmentObject var modeldata: BookmarkData
    
    @Binding var isPresent: Bool
    @Binding var isMoveMode: Bool
    //@Binding var isNewBookmark: Bool
    //@Binding var siteNameText: String
    //@Binding var url: String
    
    @State var siteNameCache: String = ""
    @State var urlCache: String = ""
    
    var onEditDone: ((_ bookmark: Bookmark)->Void)?
    
    
    
    //editor标题
    var newBookmark: String = "新建书签"
    
    
    
    
    var body: some View {
        
        ZStack {
            Color.black.opacity(isPresent ? 0.5 : 0.0).animation(.easeInOut).onTapGesture {
                self.isPresent = false
                siteNameCache = ""
                urlCache = ""
                
            }
            VStack {
                
                Text(isMoveMode ? "新建文件夹" : newBookmark)
                    .padding()
                
                TextField("名称", text: $siteNameCache)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .padding(.horizontal)
                    .disableAutocorrection(true)
                    .autocapitalization(.none)
                
                if !isMoveMode {
                    TextField("地址", text: $urlCache)
                        .textFieldStyle(RoundedBorderTextFieldStyle())
                        .padding(.horizontal)
                        .disableAutocorrection(true)
                        .autocapitalization(.none)
                }
                
                HStack {
                    Button(action: {
                        //取消键 Cancel
                        self.isPresent = false
                        siteNameCache = ""
                        urlCache = ""
                        
                    }, label: {
                        Text("Cancel")
                    })
                    
                    Spacer()
                    
                    Button(action: {
                        //确认键 Done
                        var newBookmark: Bookmark = Bookmark(date_added: Date(), last_visited: Date(), name: siteNameCache, url: isMoveMode ? nil : urlCache)
                        
                        self.isPresent = false
                        onEditDone?(newBookmark)
                        siteNameCache = ""
                        urlCache = ""
                        
                    }, label: {
                        Text("Done")
                    })
                }
                .padding(.horizontal)
                .padding(.bottom)
                
                
                
            }
            
            .background(Color(.white))
            .clipShape(RoundedRectangle(cornerRadius: 10))
            .frame(width: 300, height: 200, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
            //以后根据屏幕大小调整
            .shadow(color: .gray, radius: 10, x: -5, y: 5)
            .offset(y: isPresent ? 0 : 900)
            .animation(/*@START_MENU_TOKEN@*/.easeIn/*@END_MENU_TOKEN@*/)
        }
        
    }
}

//struct NewBookmarkEditorAlert_Previews: PreviewProvider {
//    static var previews: some View {
//        NewBookmarkEditorAlert(isPresent: .constant(true))
//    }
//}
